package com.shuyihui.animdemo;

import androidx.annotation.Nullable;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

import com.owbtgad.common.base.BaseDActivity;
import com.shuyihui.animdemo.databinding.ActivityAminBinding;

public class AminActivity extends BaseDActivity<ActivityAminBinding> {
private static String TAG = "AminActivity";


    /**
     * 如果是从现在的位置移动到别的位置，那么初始XY的值都是0，向左边，那么结束X值就是 负数，向右则是正数，上下亦是如此
     * 如果是从别的位置移动到现在的位置，那么结束XY的值都是0，其他的和上面一样
     * @param fromX 初始X位置
     * @param toX   结束X位置
     * @param fromY 初始Y位置
     * @param toY   结束Y位置
     * @param time  动画时间
     * @param view  控件
     */
    private void setAnimation(float fromX, float toX, float fromY, float toY, long time, View view) {
        /**位移动画*/
        Animation mTranslateAnimation = new TranslateAnimation(fromX, toX,fromY, toY);
        mTranslateAnimation.setDuration(time);

        /** 缩放动画*/
        Animation mScaleAnimation = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f,// 整个屏幕就0.0到1.0的大小//缩放
                Animation.INFINITE, 1.5f,
                Animation.INFINITE, 1.5f);
        mScaleAnimation.setDuration(time);
        /** 渐变动画*/
        AlphaAnimation alpha = new AlphaAnimation(1, 0);
        alpha.setDuration(time);
//        alpha.setFillAfter(true);
        /** 组合动画*/
        AnimationSet mAnimationSet=new AnimationSet(false);
        mAnimationSet.addAnimation(mScaleAnimation);
        mAnimationSet.addAnimation(alpha);
        mAnimationSet.setFillAfter(true);//true 设置动画结束后，控件位置保持不动，false 则是返回初始位置
        mAnimationSet.addAnimation(mTranslateAnimation);
        view.startAnimation(mAnimationSet);


    }

    private void setAnimation2(float fromX, float toX, float fromY, float toY, long time, View view){
        PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat("translationX", fromX, toX);
        translationX.setEvaluator(new TypeEvaluator<Float>() {
            @Override
            public Float evaluate(float v, Float o, Float t1) {
                Log.d(TAG, "evaluate:x "+o+"  t1="+t1);
                return v*t1;
            }
        });
        PropertyValuesHolder translationY = PropertyValuesHolder.ofFloat("translationY", fromY, toY);

        translationY.setEvaluator(new TypeEvaluator<Float>(){
            @Override
            public Float evaluate(float v, Float pointF, Float t1) {
                Log.d(TAG, "evaluate:y "+pointF+"  "+t1);
                return t1*v;
            }
        });
        /**同时播放两个动画**/
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 1.8f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.8f);

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, translationX, translationY,scaleX,scaleY).setDuration(time);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.setEvaluator(new TypeEvaluator<Float>() {
            @Override
            public Float evaluate(float v, Float o, Float t1) {
                Log.d(TAG, "evaluate: obj"+o+"   "+t1);
                return t1*v;
            }
        });
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                Log.d(TAG, "onAnimationStart: ");
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                Log.d(TAG, "onAnimationEnd: ");
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                Log.d(TAG, "onAnimationCancel: ");
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                Log.d(TAG, "onAnimationRepeat: ");
            }
        });
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                PointF point = (PointF) valueAnimator.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: "+valueAnimator.getAnimatedValue());
            }
        });
//        objectAnimator.start();
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.3f);
        alpha.setDuration(500);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(objectAnimator).before(alpha);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animatorSet.start();
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        final int[] h = {0};
        mBinding.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mBinding.view.post(new Runnable() {
            @Override
            public void run() {
                h[0] = mBinding.view.getHeight();
                Log.d(TAG, "run: "+mBinding.view.getHeight());
                setAnimation2(0, h[0]*1.5f, 0, -h[0]*4,1200,mBinding.view);
            }
        });
        mBinding.btn1.setOnClickListener(view -> {
            startActivity(new Intent(AminActivity.this,AnimActivity.class));
            overridePendingTransition(R.anim.login_fade_in,R.anim.anim_activity_stay);
        });
        mBinding.btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(AminActivity.this,AnimActivity.class));
                overridePendingTransition(R.anim.login_fade_out,R.anim.anim_activity_stay);
            }
        });
        mBinding.btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(AminActivity.this,AnimActivity.class));
                overridePendingTransition(R.anim.login_slide_in_right,0);
            }
        });
        mBinding.btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(AminActivity.this,AnimActivity.class));
                overridePendingTransition(R.anim.push_bottom_in,R.anim.anim_activity_stay);
            }
        });
    }
}